﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Core;
using Core.Attributes;

namespace SimpleCVSParser
{
    [ParserConfig(typeof(SimpleCVSConfig))]
    public class SimpleCVSParser:IParser<Result>
    {

        public SimpleCVSConfig Config { get; set; }
        public Result Parse()
        {
            Result result = new Result();
            List<String> keyList = new List<string>();
            List<String> valueList = new List<string>();
            string line = null;
            StreamReader file = new StreamReader(@""+Config.FileName);
            if((line = file.ReadLine()) != null)
            {
                keyList = line.Split(',').ToList();
            }

            while ((line = file.ReadLine()) != null)
            {
                valueList = line.Split(',').ToList();
                Dictionary<string, string> newDictionary = new Dictionary<string, string>();
                for (int i = 0 ; i<keyList.Count ; i++)
                {
                    newDictionary.Add(keyList[i],valueList[i]);
                }
                result.List.Add(newDictionary);
            }
            file.Close();
            return result;
        }

        public void ConvetMyConfig(object config)
        {
            Config = (SimpleCVSConfig) config;
        }

    }
}
